package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import model.OptionMessenger;
import model.UICommand;
import view.MainFrame;
import view.LogginPanel;

public class MainController implements ActionListener {
	private MainFrame mainView;
	private LogginPanel logginView;

	public MainController() {
		this.mainView = new MainFrame(this);
		LogginController logginControl = new LogginController(mainView);
		this.logginView = logginControl.getView();
		this.mainView.addComponent(logginView, logginView.getName());
		this.mainView.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		switch (cmd) {
		case UICommand.LOGGOUT_CMD:
			int answer = OptionMessenger.showQuestionDialog(
					mainView.getFrame(),
					"Bạn có chắc muốn đăng xuất khỏi tài khoản này?",
					"Xác nhận", JOptionPane.QUESTION_MESSAGE);
			if (answer == OptionMessenger.OK_OPTION) {
				mainView.changeDisplay(logginView.getName());
				logginView.setText();
			}
			break;
		case UICommand.EXIT_CMD:
			exit();
			break;
		case UICommand.POPUP_CMD:
			mainView.showPopupMenu();
			break;
		default:
			mainView.showComponent(cmd);
		}
	}

	public void exit() {
		int answer = OptionMessenger.showQuestionDialog(mainView.getFrame(),
				"Bạn có chắc muốn thoát khỏi chương trình?", "Xác nhận",
				JOptionPane.QUESTION_MESSAGE);
		if (answer == 0)
			System.exit(1);
	}

}
